package com.v2future.sjt.collegecheckingattendance.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	public static final String HEX = "0123456789abcdef";

	public static String bytesToHex(byte[] bytes) {
		if (bytes != null && bytes.length > 0) {
			StringBuilder builder = new StringBuilder(bytes.length * 2);
			for (byte b : bytes) {
				// 取出这个字节的高4位，然后与0x0f与运算，得到一个0-15之间的数据，通过HEX.charAt(0-15)即为16进制数
				builder.append(HEX.charAt((b >> 4) & 0x0f));
				// 取出这个字节的低位，与0x0f与运算，得到一个0-15之间的数据，通过HEX.charAt(0-15)即为16进制数
				builder.append(HEX.charAt(b & 0x0f));
			}

			return builder.toString();
		}
		return null;
	}

	public static byte[] hexToBytes(String hexString) {
		if (hexString != null && hexString.length() != 0) {
			int length = hexString.length() / 2;
			char[] hexChars = hexString.toCharArray();
			byte[] bytes = new byte[length];
			for (int i = 0; i < length; i++) {
				int pos = i * 2;
				bytes[i] = (byte) ((((byte) (HEX.indexOf(hexChars[pos])) << 4) | ((byte) (HEX
						.indexOf(hexChars[pos + 1])))));
			}
			return bytes;
		}

		return null;
	}

	/**
	 * Checks if is numeric.
	 *
	 * @param str
	 *            the str
	 * @return true, if is numeric
	 */
	public static boolean isNumeric(String str) {
		if (!TextUtils.isEmpty(str)) {
			Pattern pattern = Pattern.compile("-?[0-9]+.*[0-9]*");
			Matcher isNum = pattern.matcher(str);
			return isNum.matches();
		}
		return false;

	}

	public static boolean isMoneyNum(String str) {
		if (!TextUtils.isEmpty(str)) {
			Pattern pattern = Pattern
					.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
			Matcher isNum = pattern.matcher(str);
			return isNum.matches();
		}
		return false;
	}

	public static String objectToStr(Object object) {
		if (object != null) {
			return object.toString();
		}
		return "";

	}

	/**
	 * Join.
	 *
	 * @param array
	 *            the array
	 * @param separator
	 *            the separator
	 * @return the string
	 */
	public static String join(Object[] array, char separator) {
		if (array == null) {
			return null;
		}
		int arraySize = array.length;
		int bufSize = (arraySize == 0 ? 0 : ((array[0] == null ? 16 : array[0]
				.toString().length()) + 1) * arraySize);
		StringBuffer buf = new StringBuffer(bufSize);

		for (int i = 0; i < arraySize; i++) {
			if (i > 0) {
				buf.append(separator);
			}
			if (array[i] != null) {
				buf.append(array[i]);
			}
		}
		return buf.toString();
	}

	/**
	 * Push ids to string.
	 *
	 * @param list
	 *            the list
	 * @param separator
	 *            the separator
	 * @return the string
	 */
	public static String pushIdsToString(List<String> list, String separator) {
		StringBuilder stringBuilder = new StringBuilder();
		if (list != null && !list.isEmpty()) {
			int length = list.size();
			for (int i = 0; i < length; i++) {
				if (i > 0) {
					stringBuilder.append(separator);
				}
				stringBuilder.append(list.get(i));

			}
		}
		return stringBuilder.toString();

	}

	/**
	 * 按位数截取字符串，后面加上….
	 * @param hashSet
	 * @param separator
     * @return
     */
	public static String pushIdsToString(HashSet<String> hashSet,
										 String separator) {

		return pushIdsToString(new ArrayList<String>(hashSet), separator);

	}

	/**
	 * 按位数截取字符串，后面加上….
	 *
	 * @param oStr
	 *            the o str
	 * @param length
	 *            the length
	 * @return the string
	 */
	public static String cutStringByLength(String oStr, int length) {
		if (oStr != null) {
			String result = oStr;
			if (oStr.length() > length) {
				result = oStr.substring(0, length) + "…";// 包含start不包含end
			}
			return result;
		}
		return "";
	}

	/**
	 * 去掉取票时间文字和中文冒号 (取票时间：05:00-18:00).
	 *
	 * @param oStr
	 *            the o str
	 * @return the string
	 */
	public static String cutPickUpTime(String oStr) {
		String result = oStr;
		// result = result.replaceAll("[\u4e00-\u9fa5]+", "");//去掉中文
		// result = result.replace("：", "");//去掉中文冒号
		if (result.length() >= 5 && result.substring(0, 5).equals("取票时间：")) {
			result = result.substring(5, result.length());
		}
		return result;
	}

	/**
	 * 去除字符串中指定文本.
	 *
	 * @param defaultStr
	 *            the default str
	 * @param cutStr
	 *            the cut str
	 * @return the string
	 */
	public static String cutSpecialWord(String defaultStr, String cutStr) {
		String result = defaultStr;
		try {
			result = result.replaceAll(cutStr, "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}

	/**
	 * 给浮点数添加固定小数点位数.
	 *
	 * @param str
	 *            the str
	 * @param num
	 *            the num
	 * @return the string
	 */
	public static String appendFloat(String str, int num) {
		String tail = "";
		if (num > 0) {
			tail = tail + ".";
			for (int i = 0; i < num; i++) {
				tail += "0";
			}
		}
		DecimalFormat df = new DecimalFormat("###" + tail);// ###.00
		try {
			df.format(Double.parseDouble(str));
		} catch (Exception e) {
			// TODO: handle exception
			return str;
		}
		if (Double.parseDouble(str) < 1 && Double.parseDouble(str) >= 0) {
			return "0" + df.format(Double.parseDouble(str));
		} else {
			return df.format(Double.parseDouble(str));
		}
	}

	public static String formatPrice(String price) {
		if (!TextUtils.isEmpty(price)) {
			DecimalFormat fnum = new DecimalFormat("##0.00");

			return fnum.format(Double.parseDouble(price));
		}
		return "";
	}

	/**
	 * 根据身份证号获取生日信息.
	 *
	 * @author shijianting
	 * @param cardCode
	 *            身份证号
	 * @return 生日 格式xxxx-xx-xx
	 * @date 2015年4月27日 上午9:11:17
	 */
	@SuppressLint("DefaultLocale")
	public static String getBirthFromCardcode(String cardCode) {
		String code = cardCode;
		try {
			boolean isCardCode = IDCardUtil.IDCardValidate(code.toLowerCase());
			if (!isCardCode) {
				return null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
		String strYear = code.substring(6, 10);// 年份
		String strMonth = code.substring(10, 12);// 月份
		String strDay = code.substring(12, 14);// 月份
		return strYear + "-" + strMonth + "-" + strDay;
	}

	/**
	 * 格式化String列表，组成字符串，用逗号分割
	 * @param dataList
	 * @return
     */
	public static String formatStringList(List<String> dataList){
		String result = "";
		for (int i=0;i<dataList.size();i++) {
			if (i + 1 == dataList.size()) {
				result += dataList.get(i);
			}
			else {
				result += dataList.get(i) + ",";
			}
		}
		return result;
	}
}
